﻿using System;
using System.Collections.Generic;
using Less.DAL;
using Less.Core.Model;

namespace Less.SRV
{
    public class ConfigSRV : IConfigService
    {
        private readonly ConfigSettingsDAL configSettingsDAL;
        
        public ConfigSRV()
        {
            configSettingsDAL = new ConfigSettingsDAL();
        }

        public ConfigSRV(string connectionStringName)
        {
            configSettingsDAL = new ConfigSettingsDAL(new Database(connectionStringName));
        }

        public ConfigSRV(IDatabase database)
        {
            configSettingsDAL = new ConfigSettingsDAL(database);
        }

        #region IConfigService Members
        public List<ConfigSetting> GetAll()
        {
            return configSettingsDAL.SelectAll(); 
        }
        public ConfigSetting Get(int id)
        {
            return configSettingsDAL.SelectById(id);
        }
        public ConfigSetting GetByCode(string code)
        {
            if (code.IsNullOrEmpty())
            {
                throw new ArgumentNullException("code");
            }
            return configSettingsDAL.SelectByCode(code);
        }
        public ConfigSetting Create(ConfigSetting instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            configSettingsDAL.Insert(instance);
            return instance;
        }
        public void Update(ConfigSetting instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            configSettingsDAL.Update(instance);
        }
        public void Delete(int id)
        {
            var record = Get(id);
            configSettingsDAL.Delete(record);
        }
        

        public IEnumerable<ConfigSetting> GetAllTransalations()
        {
            return configSettingsDAL.SelectAllTranslations(); 
        }
        public IEnumerable<ConfigSetting> GetTransalationsByCode(string code)
        {
            return configSettingsDAL.SelectTranslationsByCode(code);
        }
        #endregion

        public ConfigSetting Write(ConfigSetting instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            configSettingsDAL.Write(instance);
            return instance;
        }
    }
}